package com.dfxy.blog.listener;

import com.dfxy.blog.dao.UserDao;
import com.dfxy.blog.entity.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;

public class LogOnActionListener implements ActionListener {

    private JTextField textFieldName;
    private JTextField textFieldPassword;

    private Connection con;
    private UserDao userDao;

    private Component parentComponent;

    public LogOnActionListener(JTextField textFieldName, JTextField textFieldPassword, Connection con, UserDao userDao, Component parentComponent) {
        this.textFieldName = textFieldName;
        this.textFieldPassword = textFieldPassword;
        this.con = con;
        this.userDao = userDao;
        this.parentComponent = parentComponent;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            User logOnResultUser = userDao.logOn(con, new User(textFieldName.getText(), textFieldPassword.getText()));
            if (logOnResultUser == null) {
                JOptionPane.showMessageDialog(parentComponent, "注册失败");
            } else {
                JOptionPane.showMessageDialog(parentComponent, "注册成功");
            }
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
}
